package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RepairItemBean;
import com.novellotus.iov.carvice.bean.RxDiagnosisAlarm;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.view.DialogChooseValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/10/10.
 */

public class OrderAlarmRecyclerAdapter extends RecyclerView.Adapter {

    private OrderAlarmAdapterInterface orderAlarmAdapterInterface;
    private RxDiagnosisAlarm rxDiagnosisAlarm;
    private Context context;
    private List<Boolean> showItemList = new ArrayList<>();

    public OrderAlarmRecyclerAdapter(RxDiagnosisAlarm rxDiagnosisAlarm, OrderAlarmAdapterInterface orderAlarmAdapterInterface, Context context) {
        this.orderAlarmAdapterInterface = orderAlarmAdapterInterface;
        this.rxDiagnosisAlarm = rxDiagnosisAlarm;
        this.context = context;
        if (rxDiagnosisAlarm != null)
            for (int i = 0; i < rxDiagnosisAlarm.getAlarms().size(); i++) {
                showItemList.add(true);
            }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_order_alarm_head, parent, false);
            return new HeadHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_order_alarm_item, parent, false);
            return new AlarmHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            initHead((HeadHolder) holder, position);
        } else {
            if (rxDiagnosisAlarm.getAlarms().get(position - 1) != null) {
                initHolder((AlarmHolder) holder, position - 1);
            }
        }
    }

    private void initHead(HeadHolder headHolder, int position) {
        headHolder.tv_plate.setText(rxDiagnosisAlarm.getPn());
        headHolder.tv_vin.setText(rxDiagnosisAlarm.getVin());
        headHolder.rlt_car_electricity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                orderAlarmAdapterInterface.openElectricity();
            }
        });
        headHolder.rlt_car_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                orderAlarmAdapterInterface.openElectricity();
            }
        });
    }

    private void initHolder(final AlarmHolder alarmHolder, final int position) {
        String cancel;
        if (rxDiagnosisAlarm.getAlarms().get(position).getOver() == 0) {
            cancel = "未清除";
        } else {
            cancel = "已清除";
        }

        alarmHolder.tv_unit.setText(rxDiagnosisAlarm.getAlarms().get(position).getName() + "(" + rxDiagnosisAlarm.getAlarms().get(position).getAlerts() + "次)" + " " + cancel);
        alarmHolder.recyclerCause.setLayoutManager(MyViewUtils.getLinearLayoutManager(context));
        alarmHolder.recyclerCause.setAdapter(new DiagnosisValueAdapter(rxDiagnosisAlarm.getAlarms().get(position).getCauses(), null));
        alarmHolder.recyclerSchemes.setLayoutManager(MyViewUtils.getLinearLayoutManager(context));
        alarmHolder.recyclerSchemes.setAdapter(new DiagnosisValueAdapter(null, rxDiagnosisAlarm.getAlarms().get(position).getSchemes()));
        alarmHolder.rlt_tittle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showItemList.set(position, !showItemList.get(position));
                notifyDataSetChanged();
            }
        });
        if (showItemList.get(position)) {
            alarmHolder.img_arrow.setBackgroundResource(R.mipmap.icon_detail_arrow_down);
            alarmHolder.ln_diagnosis.setVisibility(View.VISIBLE);
        } else {
            alarmHolder.img_arrow.setBackgroundResource(R.mipmap.icon_detail_arrow);
            alarmHolder.ln_diagnosis.setVisibility(View.GONE);
        }

    }


    @Override
    public int getItemCount() {
        return rxDiagnosisAlarm.getAlarms().size() + 1;
    }


    class HeadHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rlt_car_electricity)
        RelativeLayout rlt_car_electricity;
        @BindView(R.id.rlt_car_data)
        RelativeLayout rlt_car_data;
        @BindView(R.id.tv_plate)
        TextView tv_plate;
        @BindView(R.id.tv_vin)
        TextView tv_vin;


        HeadHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class AlarmHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_unit)
        TextView tv_unit;
        @BindView(R.id.rlt_cause)
        RelativeLayout rlt_cause;
        @BindView(R.id.rlt_schemes)
        RelativeLayout rlt_schemes;
        @BindView(R.id.tv_cause)
        TextView tv_cause;
        @BindView(R.id.tv_schemes)
        TextView tv_schemes;
        @BindView(R.id.recyclerCause)
        RecyclerView recyclerCause;
        @BindView(R.id.recyclerSchemes)
        RecyclerView recyclerSchemes;
        @BindView(R.id.ln_diagnosis)
        LinearLayout ln_diagnosis;
        @BindView(R.id.rlt_tittle)
        RelativeLayout rlt_tittle;
        @BindView(R.id.img_arrow)
        ImageView img_arrow;

        AlarmHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OrderAlarmAdapterInterface {
        void openElectricity();

        void openCarData();
    }

}
